/*
 * @Author: zy
 * @Date: 2021-10-25 17:19:37
 * @Last Modified by: zy
 * @Last Modified time: 2021-10-27 15:37:14
 */

import { request } from 'umi';

type CommonResponseType = {
  code: number;
  message: string;
};

// 1、获取用户状态列表
export type UserStateDataType = {
  code: number;
  message: string;
  data: {
    id: number;
    name: string;
  }[];
};

// 2、获取用户列表
type UserListDataType = {
  code: number;
  message: string;
  data: {
    rowNumber: number;
    list: {
      aid: number;
      name: string;
      phone: string;
      password: string;
      createTime: string;
      stateId: number;
      enclosureId: string;
      enclosureName: string;
    }[];
  };
};

// 2.1、获取用户信息列表带查询条件
type QueryParamsType = {
  searchKey?: string;
  stateId?: number;
  page: number;
  pageSize: number;
};

// 5、新增修改时获取修改用户信息
export type OptionUserInfoDataType = {
  code: number;
  message: string;
  data: {
    name: string;
    phone: string;
    password: string;
    enclosureId: string;
    enclosureName: string;
  };
};

// 6、新增修改时检验手机号是否存在接受后端传回的数据
type OptionQueryPhoneDataType = {
  code: number;
  message: string;
  data: {
    isExists: boolean;
    userInfo: {
      name: string;
      phone: string;
      password: string;
      enclosureId: string;
      enclosureName: string;
    };
  };
};

// 7、新增修改时新增/修改用户信息
type OptionSaveUserBodyType = {
  aid: number;
  name: string;
  phone: string;
  password: string;
  enclosureId?: string;
};

// 1、请求获取用户状态列表
export async function getUserStateList() {
  return request<UserStateDataType>(`/userManage/getUserStateList`, {
    method: 'GET',
  });
}

// 2、请求获取用户列表并查询
export async function getUserList(params?: QueryParamsType) {
  return request<UserListDataType>(`/userManage/getUserList`, {
    method: 'GET',
    params,
  });
}

// 3、请求删除用户
export async function delUser(body: { aid: number }) {
  return request<CommonResponseType>(`/userManage/delUser`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}

// 4、请求更改用户状态
export async function changeState(body: { aid: number }) {
  return request<CommonResponseType>(`/userManage/changeState`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}

// 5、请求新增修改时获取用户信息
export async function userInfo(params: { aid: number }) {
  return request<OptionUserInfoDataType>(`/userManage/userInfo`, {
    method: 'GET',
    params,
  });
}

// 6、请求检查手机号是否存在并返回数据
export async function checkPhone(params: { aid: number; phone: string }) {
  return request<OptionQueryPhoneDataType>(`/userManage/checkPhone`, {
    method: 'GET',
    params,
  });
}

// 7、请求新增修改用户信息
export async function saveUser(body: OptionSaveUserBodyType) {
  return request<CommonResponseType>(`/userManage/saveUser`, {
    method: 'POST',
    body: JSON.stringify(body),
  });
}

//8.下载附件
export function downloadFile(params: { enclosureId: string }) {
  return request(`/userManage/downloadFile`, {
    method: 'POST',
    body: JSON.stringify(params),
    responseType: 'blob',
  });
}

//9.移除文件
export function deleteFile(params: { enclosureId: string }) {
  return request(`/userManage/delFile`, {
    method: 'POST',
    body: JSON.stringify(params),
  });
}
